/*
 * <p>$Id: //depot/MarketData/nds/main/src/main/java/com/ebs/md/nds/management/ClientConnectionListener.java#2 $ </p>
 * <p>$Change: 75206 $ </p>
 * <p>$Author: chelck $ </p>
 * <p>$Date: 2009/07/16 $ </p>
 *
 * Copyright 2007 EBS Dealing Resources Inc.
 * All Rights Reserved
 */

package com.ebs.md.fxmonitor.management;


import javax.jms.JMSException;
import javax.management.Notification;

import org.apache.log4j.Logger;

import com.ebs.commons.util.SysDump;


import com.ebs.commons.comp.IParsedPayload;
import com.ebs.md.fxmonitor.topics.ClientTopicKeys;
import com.ebs.md.fxmonitor.topics.Keys;

public class ClientConnectionListener extends AbstractConnectionListener
                                implements ClientConnectionListenerMBean {
    private static final Logger s_logger = Logger.getLogger(SysDump.getCallerClass());

    @Override
    protected Notification createNotification(IParsedPayload payload) throws JMSException {

        Notification notification = null;

        String          clientIp        = payload.getParsedStringField(ClientTopicKeys.ClientIp);
        String          reason          = payload.getParsedStringField(ClientTopicKeys.Reason);

        notification = new Notification("Connection.Client.Error", getObjectName(), getNextSeqNo(), System.currentTimeMillis(), clientIp + ": " + reason);


        s_logger.debug("ConnectionListener.createNotification() returning: " + notification);
        return notification;
    }

    @Override
    protected boolean processPayload(IParsedPayload payload) {
        boolean         result          = false;
        boolean         gracefull       = payload.getParsedBooleanField(ClientTopicKeys.Gracefull);
        String          type            = payload.getParsedStringField(Keys.Type);

        result = type.equalsIgnoreCase(Keys.MessageTypes.client_disconnect.name()) && ! gracefull;
        s_logger.debug("ConnectionListener.processPayload() returning: " + result);
        return result;
    }
}
